﻿using Microsoft.AspNetCore.Mvc;
using RobosimAPI.Class;

namespace RobosimAPI.Controllers
{
	[Route("back/Api2/Client/startCheck")]
	public class StartCheckController : ControllerBase
	{
		[HttpPost]
		public IActionResult StartCheck([FromForm] StartCheckModel model)
		{
			Account account = new Account(model.user_id);
			int[] joints = account.GetAllJoinedEvents();
			if (!Array.Exists(joints, element => element == model.group_id))
			{
				return Ok(new ErrorResponse() { errmsg = "not apply tips", status = -1 });
			}
			Round round = new Round(model.round_id);
			long now = Tools.GetTimestamp();
			if (round.EndTime < now)
			{
				return Ok(new ErrorResponse() { errmsg = "The game is over", status = -1 });
			}
			if (round.StartTime > now)
			{
				return Ok(new ErrorResponse() { errmsg = "This game has not started yet", status = -1 });
			}
			EventMap eventMap = new EventMap(round.MapId);
			StartCheckResponse response = new StartCheckResponse();
			response.status = 1;
			response.data = new Data();
			response.data.mapInfo = new MapInfo();
			response.data.mapInfo.map_id = eventMap.Id;
			response.data.mapInfo.map_url = eventMap.MapUrl;
			response.data.mapInfo.map_md5 = eventMap.MapMd5;
			response.data.mapInfo.map_password = eventMap.MapPassword;
			response.data.mapInfo.file_encrypt = eventMap.FileEncrypt;
			return Ok(response);
		}
		public class StartCheckModel
		{
			public int user_id { get; set; }
			public int group_id { get; set; }
			public int round_id { get; set; }
		}
		public class MapInfo
		{
			public int map_id { get; set; }
			public string map_url { get; set; }
			public string map_md5 { get; set; }
			public string map_password { get; set; }
			public int file_encrypt { get; set; }
		}
		public class Data
		{
			public MapInfo mapInfo { get; set; }
		}
		public class StartCheckResponse
		{
			public int status { get; set; }
			public Data data { get; set; }
		}

	}
}
